home *** CD-ROM | disk | FTP | other *** search
- Path: news.microsoft.com!news
- From: a-cnadc@microsoft.com (Dann Corbit)
- Newsgroups: comp.lang.c
- Subject: Re: What's better & why
- Date: 22 Feb 1996 19:31:58 GMT
- Organization: Microsoft Corporation
- Message-ID: <4gigbe$t4m@news.microsoft.com>
- References: <31297C5A.E6C@connix.com> <4gf4s6$fl0@kannews.ca.newbridge.com>
- NNTP-Posting-Host: 157.57.171.202
- Mime-Version: 1.0
- X-Newsreader: WinVN 0.93.14
-
- In article <4gf4s6$fl0@kannews.ca.newbridge.com>, gminer@Newbridge.COM says...
- >
- >>I was just curious what you all though about the following code.
- >>Please tell me what is better (faster/Smaller) and why. Or does it make
- >>any difference at all. I just though about this while I was programming
- >>today?
- >>
- >>example 1:
- >> val = 1;
- >> if( what ever...)val = 0;
- >>
- >>or
- >>example 2:
- >> if( what ever... )val = 0;
- >> else val = 1;
- >
- >What an interesting question :) I'm going to go home, code this and then
- >decompile it into ASM to see if it does it this way (my compiler may be
- >smart, though).
- >
- >If the comiler was dumb, you would get something like the following ASM
- >instructions:
- >
- >example 1:
- > mov val, 1
- > cmp what, ever
- > jne End
- > mov val, 0
- > End:
- >
- >2 memory moves, 1 compare, one conditional jump. Fewer instructions =
- >smaller code. 3 or 4 instructions executed.
- >
- >example 2:
- > cmp what, ever
- > jne End
- > mov val, 0
- > jmp Finish
- > End: mov val 1
- > Finish:
- >
- >2 memory moves, 1 compare, one conditional jump, on unconditional jump.
- >
- >Slightly larger code, one instruction, wee :) Again, 3 or 4 instrucitons
- >executed.
- >
- >But: with the worst case in example 1, you are executing an extra move,
- >and with the worst case in example 2, you are executing an extra
- >unconditional jump. Which is faster? I don't have my ASM handbook here so
- >I couldn't tell you ;) If I had to guess, I'd say the jmp would be faster
- >becuase your val data wouldn't be registers and all.
- >
- >So, if I wanted speed, like I usually do, and I was willing to pay the
- >extra few bytes in code size, I'd stay with example 2.
- >
- >Now, I'm no asm genious, and I don't know how smart my compiler is. It
- >may well realize what you are doing and do things an even better way.
- >
- >I'm still tempted to go home and code it in c, and see what the asm turns
- >out to be :)_
- >
- >Thanx for the interesting question :)
- Your answer depends upon the particular architecture, compiler, and optimizer
- setting for one system. It may be that something that's faster on one system
- is slower on another. It also can occur that saving one or two cycles can
- disable a global optimization that would save much more. Back to the original
- example:
- >>example 1:
- >> val = 1;
- >> if( what ever...)val = 0;
- >>
- >>or
- >>example 2:
- >> if( what ever... )val = 0;
- >> else val = 1;
- Example 1 sometimes does two assignments. Example 2 only does one. If
- "what ever..." is usually true, then two assignments are usually done.
- ( for example 1, that is.)
-
- 90% of the time, it is better to make code clear and understandable than
- to tweak out a couple extra cycles. Using a profiler, identify the hot
- spots in your code that need speed, and tweak them. Often, cranking
- up the optimization level does a pretty good job of optimizing for us.
-
- 80% of software cost is maintenance.
- --
- The opinions expressed in this message are my own personal views
- and do not reflect the official views of Microsoft Corporation.
-
-